/**
 * 请求模块
 */

import axios from "axios"
import store from '@/store/'
import JSONbig from 'json-bigint'

//请求拦截器

const request = axios.create({
    baseURL:'http://ttapi.research.itcast.cn/',

    transformResponse:[function(data){
        try {
            
            return JSONbig.parse(data)
        } catch (error) {
            //console.log('转换失败',error)

            return data
        }
    }]
})


//响应拦截器

request.interceptors.request.use(function(config){

    const { user } = store.state
    if(user){
        config.headers.Authorization = `Bearer ${user.token}`
    }
    return config
},function(error){
    return Promise.reject(error)
})


//导出
export default request